package com.man.control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.man.daoService.DaoService;
import com.man.entity.Friends;
import com.man.entity.User;

public class FriendsManage {
	private DaoService service=new DaoService();
	public void insert()
	{
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		Friends friends=new Friends();
		friends.setUserName(user.getUserName());
		friends.setName(getName());
		friends.setPhone(getPhone());
		friends.setEmail(getEmail());
		friends.setWorkplace(getWorkplace());
		friends.setPlace(getPlace());
		friends.setQQ(getQQ());
		Boolean b=service.friendsInsert(friends);
		try {
			ServletActionContext.getResponse().getWriter().print(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
			
	public void select()
	{
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		Friends friends=new Friends();
		friends.setUserName(user.getUserName());
		List<Friends> list=service.friendsSelect(friends);
		
		JSONArray jsonObject=JSONArray.fromObject(list);
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(jsonObject);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void oneSelect()
	{
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		Friends friends=new Friends();
		friends.setUserName(user.getUserName());
		friends.setName(getName());
		Friends friends2=service.friendsOneSelect(friends);
		JSONObject jsonObject=JSONObject.fromObject(friends2);
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(jsonObject);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delect()
	{	
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		Friends friends=new Friends();
		friends.setUserName(user.getUserName());
		friends.setName(getName());
		Boolean b=service.friendsdelect(friends);
		try {
			ServletActionContext.getResponse().getWriter().print(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
public void updata()
{	
	User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
	Friends friends=new Friends();
	friends.setUserName(user.getUserName());
	friends.setName(getName());
	friends.setPhone(getPhone());
	friends.setPlace(getPlace());
	friends.setQQ(getQQ());
	friends.setWorkplace(getWorkplace());
	friends.setEmail(getEmail());
	Boolean b=service.friendsupdata(friends);
	try {
		ServletActionContext.getResponse().getWriter().print(b);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
	private String userName;
	private String name;
	private String phone;
	private String email;
	private String workplace;
	private String place;
	private String QQ;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getWorkplace() {
		return workplace;
	}
	public void setWorkplace(String workplace) {
		this.workplace = workplace;
	}
	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}
	public String getQQ() {
		return QQ;
	}
	public void setQQ(String qQ) {
		QQ = qQ;
	}
}
